package com.wenyl.juc.thread;

/**
 * Thread是类，Runnable是接口
 * Thread实现了Runnable接口
 *
 *
 * @author Swimming Dragon
 * @description: TODO
 * @date 2024年11月05日 22:16
 */
public class ThreadAndRunnable {
    public void testThread(){
        MyThread th1 = new MyThread();
        MyThread th2 = new MyThread();
        MyThread th3 = new MyThread();
        th1.start();
        th2.start();
        th3.start();

    }

    public void testRunnable(){
        MyThread1 th1 = new MyThread1();
        Thread t1 =  new Thread(th1);
        Thread t2 =  new Thread(th1);
        Thread t3 =  new Thread(th1);
        t1.start();
        t2.start();
        t3.start();
    }
}

class MyThread extends Thread{
    private int ticket = 10;
    @Override
    public void run() {
        while(true){
            if(ticket > 0){
                System.out.println(Thread.currentThread().getName()+"卖出第"+(10- ticket-- +1)+"张票");
            }
        }
    }
}
class MyThread1 implements Runnable{

    private int ticket = 10;
    @Override
    public void run() {
        while(true){
            if(ticket > 0){
                System.out.println(Thread.currentThread().getName()+"卖出第"+(10- ticket-- +1)+"张票");
            }
        }
    }
}